package com.shopng.test.server;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.geode.cache.Region;
import org.apache.geode.pdx.JSONFormatter;
import org.apache.geode.pdx.PdxInstance;
import org.springframework.util.Assert;

import com.shopng.geode.api.ClientService;
import com.shopng.geode.common.IPAddress;
import com.shopng.geode.common.JsonUtils;
import com.shopng.utils.ConfigHelper;
import com.shopng.utils.Util;

public class ImportData
{
  
  public static void main(String[] args)
  {
    ConfigHelper.initConfig();
    
    ClientService cs = new ClientService().getInstance();
    IPAddress ia = new IPAddress(ConfigHelper.getDefaultLocatorAdddress(), ConfigHelper.getDefaultLocatorPort());
    cs.initCacheWithPool(new IPAddress[] { ia });
    
    @SuppressWarnings("unchecked")
    Region<String,PdxInstance> r = (Region<String,PdxInstance>)cs.getRegion("/goods");
    
    try
    {
      Map<String,String> map = Util.parseCsv("src/test/resources/goods.csv", new String[]{"goods_id"}, ',', '"');
      Assert.notNull(r);
      Map<String,PdxInstance> dataMap = new HashMap<String,PdxInstance>();
      Iterator<String> it = map.keySet().iterator();
      int i = 0;
      while(it.hasNext())
      {
        String key = it.next();
        String json = map.get(key);
        PdxInstance pdxInstance = JSONFormatter.fromJSON(json);
        dataMap.put(key, pdxInstance);
        i++;
        if(i%1000 == 0)
        {
          r.putAll(dataMap);
          dataMap.clear();
        }
      }
      if(dataMap.size()>0)
      {
        r.putAll(dataMap);
      }
    }
    catch (IOException e)
    {
      e.printStackTrace();
    }
    
  }
  
}
